﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace Rabbit.Utils
{
    public static class DateTimeHelper
    {
        public static string GetNormalDateStringFromDateTimeType(DateTime date)
        {
            if (DateTime.Now.Subtract(TimeSpan.FromDays(2)).Date.CompareTo(date.Date) <= 0)
            {
                if (DateTime.Now.Subtract(TimeSpan.FromDays(2)).Date == date.Date)
                {
                    return "前天 " + date.ToLongTimeString();
                }
                else if (DateTime.Now.Subtract(TimeSpan.FromDays(1)).Date == date.Date)
                {
                    return "昨天 " + date.ToLongTimeString();
                }
                else
                {
                    TimeSpan ts = DateTime.Now.Subtract(date);
                    if (ts.Hours == 0)
                    {
                        if (ts.Minutes == 0)
                        {
                            return ts.Seconds + "秒前";
                        }
                        else
                        {
                            return ts.Minutes + "分钟前";
                        }
                    }
                    else
                    {
                        if (ts.Minutes == 0)
                        {
                            return ts.Hours + "小时前";
                        }
                        else
                        {
                            return ts.Hours + "小时" + ts.Minutes + "分钟前";
                        }
                    }
                }
            }
            else
            {
                return date.ToString("yyyy-MM-dd HH:mm");
            }
        }
        public static string GetNormalDateStringFromString(string timeString, string format = "ddd MMM dd HH:mm:ss zz00 yyyy")
        {
            return GetNormalDateStringFromDateTimeType(GetDateTimeFromString(timeString,format));
        }
        public static DateTime GetDateTimeFromString(string timeString, string format = "ddd MMM dd HH:mm:ss zz00 yyyy")
        {
            return DateTime.ParseExact(timeString, format, CultureInfo.InvariantCulture);
        }
#if UnitiTest
        private void UnitTest()
        {
            DateTime dtYesterday = DateTime.Now.Subtract(TimeSpan.FromDays(1)).AddHours(1);
            DateTime dtBeforeYesterday = DateTime.Now.Subtract(TimeSpan.FromDays(2)).AddHours(2);
            DateTime dtToday3Hour = DateTime.Now.Subtract(TimeSpan.FromHours(3)).AddMinutes(5);
            DateTime dtToday3Min = DateTime.Now.Subtract(TimeSpan.FromMinutes(3));
            DateTime dtToday3Sec = DateTime.Now.Subtract(TimeSpan.FromSeconds(3));
            DateTime dtLastYear = new DateTime(2009, 12, 26);
            string yersterday = DateTimeConverter.GetNormalDateString(dtYesterday);
            string BeforeYesterday = DateTimeConverter.GetNormalDateString(dtBeforeYesterday);
            string Today3Hour = DateTimeConverter.GetNormalDateString(dtToday3Hour);
            string Today3Min = DateTimeConverter.GetNormalDateString(dtToday3Min);
            string Today3Sec = DateTimeConverter.GetNormalDateString(dtToday3Sec);
            string LastYear = DateTimeConverter.GetNormalDateString(dtLastYear);
        }
#endif
    }
}
